package com.example.warehouse.bus.service.impl;

import com.example.warehouse.bus.dao.persist.repository.IBusCustomerRepository;
import com.example.warehouse.bus.dao.persist.repository.IBusGoodsRepository;
import com.example.warehouse.bus.pojo.entity.BusCustomer;
import com.example.warehouse.bus.pojo.entity.BusGoods;
import com.example.warehouse.bus.pojo.entity.BusProvider;
import com.example.warehouse.bus.pojo.param.BusCustomerAddNewParam;
import com.example.warehouse.bus.pojo.param.BusGoodsAddNewParam;
import com.example.warehouse.bus.pojo.vo.BusGoodsStandardVO;
import com.example.warehouse.bus.pojo.vo.BusProviderStandardVO;
import com.example.warehouse.bus.service.IBusGoodsService;
import com.example.warehouse.common.ex.ServiceException;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Linger
 * @date 2023-07-07 8:59
 * @desciption:处理供应商数据的业务实体类
 */
@Slf4j
@Service
public class BusGoodsServiceImpl implements IBusGoodsService {
    @Value("${warehouse.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    @Autowired
    private IBusGoodsRepository busGoodsRepository;

    public BusGoodsServiceImpl() {
        log.info("创建控制器对象:BusGoodsServiceImpl ");
    }

    @Override
    public void addNewBusGoods(BusGoodsAddNewParam busGoodsAddNewParam) {
        log.debug("开始处理【添加商品】的业务，参数：{}", busGoodsAddNewParam);
        // 检查供应商id商品名的组合是否被占用
        {
            String goodsName = busGoodsAddNewParam.getGoodsName();
            Long providerId = busGoodsAddNewParam.getProviderId();
            int count = busGoodsRepository.countByGoodsNameAndProviderId(goodsName, providerId);
            if (count > 0) {
                String message = "添加商品失败，商品名已经被占用！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
            }
        }
        // 检查批准文号是否被占用
        {
            String promitCode = busGoodsAddNewParam.getPromitCode();
            int count = busGoodsRepository.countByPromitCode(promitCode);
            if (count > 0) {
                String message = "添加商品失败，商品批准文号已经被占用！";
                log.warn(message);
                throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
            }
        }


        BusGoods busGoods = new BusGoods();
        BeanUtils.copyProperties(busGoodsAddNewParam, busGoods);
        int rows =busGoodsRepository.insert(busGoods);
        if (rows != 1) {
            String message = "添加商品失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【根据ID删除商品】的业务，参数：{}", id);
        Object queryResult = busGoodsRepository.getStandardById(id);
        if (queryResult == null) {
            String message = "删除商品失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        log.debug("即将执行删除数据，参数：{}", id);
        int rows = busGoodsRepository.deleteById(id);
        if (rows != 1) {
            String message = "删除商品失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public void update(BusGoods busGoods) {
        log.debug("开始处理【修改商品】的业务，参数：{}",busGoods);
        Object queryResult = busGoodsRepository.getStandardById(busGoods.getId());
        if (queryResult == null) {
            String message = "修改商品失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        int rows =busGoodsRepository.update(busGoods);
        if (rows != 1) {
            String message = "修改商品失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    @Override
    public BusGoodsStandardVO getStandardByGoodsId(Long id) {
        log.debug("开始处理【根据id查询商品】的业务，参数：{}", id);
        return busGoodsRepository.getStandardByGoodsId(id);

    }

    @Override
    public PageData<BusGoodsStandardVO> queryByKeyWord(Integer pageNum,String goodsName, Long providerId, String promitCode, String producePlace, String productCode, String description) {
        log.debug("开始处理【模糊查询】的业务，参数：{},{},{},{},{},{} ",goodsName, providerId, promitCode, producePlace, productCode, description);
        return busGoodsRepository.queryByKeyWord(pageNum, defaultQueryPageSize,goodsName, providerId, promitCode, producePlace, productCode, description);
    }

    @Override
    public PageData<BusGoods> list(Integer pageNum) {
        log.debug("开始处理【查询商品列表】的业务，页码：{}", pageNum);
        PageData<BusGoods> pageData = busGoodsRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<BusGoods> listAsc(Integer pageNum) {
        log.debug("开始处理【升序查询商品列表】的业务，页码：{}", pageNum);
        PageData<BusGoods> pageData = busGoodsRepository.listAsc(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<BusGoods> listDesc(Integer pageNum) {
        log.debug("开始处理【降序查询商品列表】的业务，页码：{}", pageNum);
        PageData<BusGoods> pageData = busGoodsRepository.listDesc(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<BusGoods> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询商品列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<BusGoods> pageData = busGoodsRepository.list(pageNum, pageSize);
        return pageData;
    }

    @Override
    public void setGoodsDisable(Long id) {
        updateEnableByGoodsId(id, 0);
    }

    @Override
    public void setGoodsEnable(Long id) {
        updateEnableByGoodsId(id, 1);
    }

    private void updateEnableByGoodsId(Long id, Integer enable) {
        log.debug("开始处理【{}商品】的业务，ID：{}，目标状态：{}", ENABLE_TEXT[enable], id, enable);
        BusGoodsStandardVO queryResult = busGoodsRepository.getStandardByGoodsId(id);
        if (queryResult == null) {
            String message = ENABLE_TEXT[enable] + "商品失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        if (queryResult.getEnable().equals(enable)) {
            String message = ENABLE_TEXT[enable] + "商品失败，当前用户已经处于"
                    + ENABLE_TEXT[enable] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }

        BusGoods user = new BusGoods();
        user.setId(id);
        user.setEnable(enable);
        log.debug("即将修改数据，参数：{}", user);
        int rows = busGoodsRepository.updateByGoodsId(user);
        if (rows != 1) {
            String message = ENABLE_TEXT[enable] + "商品失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }


}
